<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Frame;

use Srv\Libs\ConfModel\RouteConf;

final class RequestHeader
{
    private static ?RouteConf $RouteConf        = null;

    /**
     * @param RouteConf $RouteConf
     * @return void
     * init
     */
    final public static function init(RouteConf $RouteConf):void
    {
        self::$RouteConf    = $RouteConf;
    }

    /**
     * @param string $itemName
     * @return mixed|null
     * getDataByHeaderItem
     */
    final public static function getDataByHeaderItem(string $itemName)
    {
        if(strlen($itemName) < 1 || !(self::$RouteConf instanceof RouteConf)) return null;
        return EnvData::getByHeaderRouteItem($itemName, self::$RouteConf);
    }

    /**
     * @return string
     * getDevId
     */
    final public static function getDevId():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_ID);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @param string $devCodeName
     * @param string $devBrandName
     * @param string $devModelName
     * @return string
     * getDevName
     */
    final public static function getDevName(string &$devCodeName = '', string &$devBrandName = '', string &$devModelName = ''):string
    {
        $headerVal      = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_NAME);
        if(!is_string($headerVal)) return '';
        $devNameList    = explode(';', $headerVal, 3);
        $devCodeName    = trim($devNameList[0]??'');
        $devBrandName   = trim($devNameList[1]??'');
        $devModelName   = trim($devNameList[2]??'');
        return $headerVal;
    }

    /**
     * @return string
     * getDevSystem
     */
    final public static function getDevSystem():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_SYSTEM);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return int
     * getDevTime
     */
    final public static function getDevTime():int
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_TIME);
        return is_string($headerVal) ? intval($headerVal) : 0;
    }

    /**
     * @return string
     * getDevVersion
     */
    final public static function getDevVersion():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_VERSION);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getDevLang
     */
    final public static function getDevLang():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_LANG);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getDevNet
     */
    final public static function getDevNet():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_NET);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return int
     * getDevOffset
     */
    final public static function getDevOffset(bool &$isSet = false):int
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_OFFSET);
        $isSet      = !is_null($headerVal);
        return is_string($headerVal) ? intval($headerVal) : 0;
    }

    /**
     * @return string
     * getAppKey
     */
    final public static function getAppKey():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_APP_KEY);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getAppPlatform
     */
    final public static function getAppPlatform():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_APP_PLATFORM);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return int
     * getAppNonce
     */
    final public static function getAppNonce():int
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_APP_NONCE);
        return is_string($headerVal) ? intval($headerVal) : 0;
    }

    /**
     * @return string
     * getAppToken
     */
    final public static function getAppToken():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_APP_TOKEN);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getDataByHeaderItem
     */
    final public static function getSig():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_SIG);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getKey
     */
    final public static function getKey():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_KEY);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getSigSkip
     */
    final public static function getSigSkip():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_SIG_SKIP);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @param int $width
     * @param int $height
     * @return string
     * getDevScreen
     */
    final public static function getDevScreen(int &$width = 0, int &$height = 0):string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_SCREEN);
        if(!is_string($headerVal) || strlen($headerVal) < 1) return '';
        if(preg_match('/^(\d{3,6})x(\d{3,6})$/', $headerVal, $match)){
            $width          = intval($match[1]);
            $height         = intval($match[2]);
        }
        return $headerVal;
    }

    /**
     * @param string $lat
     * @param string $lng
     * @return string
     * getDevLocation
     */
    final public static function getDevLocation(string &$lat = '', string &$lng = ''):string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_DEV_LOCATION);
        if(!is_string($headerVal) || strlen($headerVal) < 1) return '';
        if(preg_match('/^((-)?\d{1,3}\.\d{1,12}),((-)?\d{1,2}\.\d{1,12})$/', $headerVal, $match)){
            $lng            = trim($match[1]);
            $lat            = trim($match[3]);
        }
        return $headerVal;
    }

    /**
     * @param string $channelName
     * @param string $bundleId
     * @return string
     * getAppChannel
     */
    final public static function getAppChannel(string &$channelName = '', string &$bundleId = ''):string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_APP_CHANNEL);
        if(!is_string($headerVal) || strlen($headerVal) < 1) return '';
        if(preg_match('/^([\da-zA-Z\-_]{3,16})\/([\da-zA-Z\-.]{5,32})$/', $headerVal, $match)){
            $channelName    = trim($match[1]);
            $bundleId       = trim($match[2]);
        }else{
            $channelName    = trim($headerVal);
            $bundleId       = '';
        }
        return $headerVal;
    }

    /**
     * @return string
     * getContentType
     */
    final public static function getContentType():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_CONTENT_TYPE);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getScheme
     */
    final public static function getScheme():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_CLIENT_SCHEME);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getHost
     */
    final public static function getHost():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_CLIENT_HOST);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return int
     * getPort
     */
    final public static function getPort():int
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_CLIENT_PORT);
        return is_string($headerVal) ? intval($headerVal) : 0;
    }

    /**
     * @return string
     * getIp
     */
    final public static function getIp():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_CLIENT_IP);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getForwardedFor
     */
    final public static function getForwardedFor():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_X_FORWARDED_FOR);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getUserAgent
     */
    final public static function getUserAgent():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_USER_AGENT);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getAcceptLanguage
     */
    final public static function getAcceptLanguage():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_ACCEPT_LANGUAGE);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getReferer
     */
    final public static function getReferer():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_REFERER);
        return is_string($headerVal) ? $headerVal : '';
    }

    /**
     * @return string
     * getAccept
     */
    final public static function getAccept():string
    {
        $headerVal  = self::getDataByHeaderItem(ResponseMain::HEADER_ACCEPT);
        return is_string($headerVal) ? $headerVal : '';
    }
}
